package com.cpedu;

import com.cpedu.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMybatis {
    public static void main(String[] args) throws IOException {
        //1获取SqlSessionFactory
        String resource = "mybatis-spring.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2执行sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3执行sql
        List<User> userList = sqlSession.selectList("test.selectAll");
        for (Object o :userList) {
            System.out.println(o);
        }

        //释放资源
        sqlSession.close();

    }
}
